// index.js
// 获取应用实例
const app = getApp()

const calcProvidentBaseValList = [0, 36000, 144000, 300000, 420000, 660000, 960000]; // 基数
const calcProvidentPropList = [3, 10, 20, 25, 30, 35, 45]; // 百分之, 税率
const calcProvidentQuickDeducList = [0, 2520, 16920, 31920, 52920, 85920, 181920]; // 速算扣除数

const calcProvidentFund = function (annualSalary) {
  var partIndex = 0;  // 税率级别(下标)
  for (var index = 1; index < calcProvidentBaseValList.length; ++index) {
    if (annualSalary >= calcProvidentBaseValList[index]) {
      partIndex++;
    }
  }
  const baseval = calcProvidentBaseValList[partIndex];
  const prop = calcProvidentPropList[partIndex];
  const quickDeduc = calcProvidentQuickDeducList[partIndex];
  return (annualSalary)*(prop/100) + quickDeduc;
};

Page({
  data: {
    motto: 'Hello World',
    texts: 'ddd',
    // txtResult: 'init',
    canCalc: true,
    canClear: true,
    // intResult: 0,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName'), // 如需尝试获取用户信息可改为false
    annualIndividualIncomeTax: 0,  // 年度个人所得税
    annualSalary: 0, // 年收入
  },
  // calcAnnualIndividualIncomeTax: 0,
  monthlySalary: 0,     // 每月工资
  providentFundProp: 0,    // 公积金比例,  百分号
  socialInsurancePaymentbase: 0,  // 社保缴纳基数, 大整数
  specialDeductionAmount: 0, // 专项扣除金额, 
  setMonthlySalary: function (e) {
    this.monthlySalary = e.detail.value;
  },
  setProvidentFundProp: function (e) {
    this.providentFundProp = e.detail.value;
  },
  setSocialInsurancePaymentbase: function (e) {
    this.socialInsurancePaymentbase = e.detail.value;
  },
  setSpecialDeductionAmount: function (e) {
    this.specialDeductionAmount = e.detail.value;
  },
  calc() {
    const locMonthlySalary = parseFloat(this.monthlySalary);
    const locProvidentFundProp = parseFloat(this.providentFundProp);
    const locSocialInsurancePaymentbase = parseFloat(this.socialInsurancePaymentbase);
    const locSpecialDeductionAmount = parseFloat(this.specialDeductionAmount);

    console.log('开始计算...')
    console.log('输入数据为: 每月工资:' + this.monthlySalary + "," + '公积金比例:' + this.providentFundProp
      + ',' + '社保缴纳基数:' + this.socialInsurancePaymentbase + "," + '月度专项扣除金额:' + this.specialDeductionAmount
    );
    const rawAnnualSalary = 12 * locMonthlySalary;
    
    // 20000 , 5% , 15000 ,  1500
    // 总缴纳 = 社保缴纳基数*（0.08（养老）+0.02（医保）+0.01（失业）+公积金比例） = 2400
    //  + 1500 = 3900
    // 240000 - 3900 = ... == 18420, 16920
    const annualTotalPay = 12 * locSocialInsurancePaymentbase*(0.08 + 0.02 + 0.01 + locProvidentFundProp/100);
    console.log('公积金比例:' + locProvidentFundProp/100);
    console.log('年度总缴纳:' + annualTotalPay);
    // 扣除后待计算的收入
    const inCalAnnualSalary = rawAnnualSalary - annualTotalPay - 60000 - locSpecialDeductionAmount * 12; // 年度基本扣除费用
    console.log('扣除后待计算的收入:' + inCalAnnualSalary);
    // 个人所得税
    const provident = calcProvidentFund(inCalAnnualSalary);
    this.setData({
      annualIndividualIncomeTax: provident,
      annualSalary: rawAnnualSalary
    })
  },
  clear() {
    console.log('click clear button');
    const nextVal = 0;
    this.setData({
      annualIndividualIncomeTax: nextVal,
    })
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  getUserInfo(e) {
    // 不推荐使用getUserInfo获取用户信息，预计自2021年4月13日起，getUserInfo将不再弹出弹窗，并直接返回匿名的用户个人信息
    console.log(e)
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})
